/*
 * @Author: xiaosihan
 * @Date: 2024-10-02 14:09:50
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2024-10-03 17:56:17
 */

import isString from 'lodash/isString';
import isObject from 'lodash/isObject';
import { clone } from 'ramda';
import { GoodsData } from '#/type';
import outlineGenerate from './outlineGenerate';
import utils from './utils';
import optimizeGlb from './optimizeGlb';
import utilsServer from '@/api/services/utilsServer';
import { isEmpty } from 'lodash';

class GoodsUtils {
  keys: Array<keyof GoodsData> = [
    'colors',
    'component',
    'mockupUrls',
    'miniProgramMockupUrls',
    'size',
    'sizeInfo',
    'thumbs',
    'sizePrice',
    'suitFor',
  ];
  //将 商品数据中的 json 字符串转成json格式
  tojson(goodsData: GoodsData) {
    const newGoodsData = clone(goodsData);
    this.keys.map((key) => {
      let value = {};
      do {
        value = newGoodsData[key];
        if (isString(value)) {
          newGoodsData[key] = JSON.parse(value as string) as never;
        }
      } while (isString(value));
    });

    //保持数据结构
    if (!newGoodsData.thumbs) {
      newGoodsData.thumbs = [];
    }

    return newGoodsData;
  }

  //将 商品数据中的 json 转成json字符串
  toString(goodsData: GoodsData) {
    const newGoodsData = clone(goodsData);
    this.keys.map((key) => {
      const value = newGoodsData[key];
      if (isObject(value)) {
        newGoodsData[key] = JSON.stringify(value) as never;
      }
    });
    return newGoodsData;
  }

  //获取某个部位的uv图,格式是svg
  get_uv_url(goodsData: GoodsData, componentKey: string) {
    const { modalSize, component } = goodsData;
    const com = component.find((item) => item.key === componentKey)!;
    const { uv_3XS, uv_2XS, uv_XS, uv_S, uv_M, uv_L, uv_XL, uv_2XL, uv_3XL, uv_4XL, uv_5XL, uv_6XL } = com;
    let uv_url = uv_3XS || uv_2XS || uv_XS || uv_S || uv_M || uv_L || uv_XL || uv_2XL || uv_3XL || uv_4XL || uv_5XL || uv_6XL || "";

    switch (modalSize) {
      case "3XS":
        uv_url = uv_3XS || uv_url;
        break;
      case "2XS":
        uv_url = uv_2XS || uv_url;
        break;
      case "XS":
        uv_url = uv_XS || uv_url;
        break;
      case "S":
        uv_url = uv_S || uv_url;
        break;
      case "M":
        uv_url = uv_M || uv_url;
        break;
      case "L":
        uv_url = uv_L || uv_url;
        break;
      case "XL":
        uv_url = uv_XL || uv_url;
        break;
      case "2XL":
        uv_url = uv_2XL || uv_url;
        break;
      case "3XL":
        uv_url = uv_3XL || uv_url;
        break;
      case "4XL":
        uv_url = uv_4XL || uv_url;
        break;
      case "5XL":
        uv_url = uv_5XL || uv_url;
        break;
      case "6XL":
        uv_url = uv_6XL || uv_url;
        break;
      default:
        break;
    }

    return uv_url;

  }


  //数据修复并对数据进行补充
  //主要为小程序编辑区提前处理一些数据信息
  async repair(goodsData: GoodsData) {
    //新加的字段修复小程序用的上身效果glb
    if (isEmpty(goodsData.miniProgramMockupUrls) && !isEmpty(goodsData.mockupUrls)) {
      goodsData.miniProgramMockupUrls = await Promise.all(goodsData.mockupUrls.map((mockupUrl) => {
        return new Promise<string>(async (resolve, reject) => {
          const glb = await utils.downloadFile(mockupUrl, "1.glb");
          const miniGLB = await optimizeGlb.optimize(glb);
          resolve(await utilsServer.uploadOss(miniGLB));
        });
      }));
    }

    //后加的字段需要修复小程序用的模型glb
    if (isEmpty(goodsData.miniProgramModelUrl) && !isEmpty(goodsData.modelUrl)) {
      const glb = await utils.downloadFile(goodsData.modelUrl, "1.glb");
      const miniGLB = await optimizeGlb.optimize(glb);
      goodsData.miniProgramModelUrl = await utilsServer.uploadOss(miniGLB);
    }

    // 补充小程序专用的轮廓图
    for (let com of goodsData.component) {

      let uv_url = this.get_uv_url(goodsData, com.key);

      const {
        print_area_height, print_area_width, print_area_top, print_area_left,
        clip_print_area_top, clip_print_area_left, clip_print_area_width, clip_print_area_height
      } = com;
      const markRact = { x: print_area_left, y: print_area_top, width: print_area_width, height: print_area_height };
      const clipMarkRect = { x: clip_print_area_left, y: clip_print_area_top, width: clip_print_area_width, height: clip_print_area_height };

      if (uv_url) {
        if (!com.oulineMark) {
          com.oulineMark = await outlineGenerate.generateOulineMark(uv_url);
        }
        if (!com.designMark) {
          com.designMark = await outlineGenerate.generateDesignMark(uv_url, markRact, clipMarkRect);
        }
        if (!com.outlineDash) {
          com.outlineDash = await outlineGenerate.generateOutlineDash(uv_url, markRact, clipMarkRect);
        }
        if (!com.printlineDash) {
          com.printlineDash = await outlineGenerate.generatePrintlineDash(uv_url, markRact, clipMarkRect);
        }
      }
    }

    return goodsData;
  }


}

const goodsUtils = new GoodsUtils();

export default goodsUtils;
